const path = require('path')
const { APP_PORT, SYS_LOCATION } = require('../config/config.development');
const { bannerAdd, bannerUpdate, bannerDelete, bannerDetail, bannerList, getBanner } = require('../service/banner.service')
class BannerController {
    // 轮播图图片上传
    async upload(ctx) {
        ctx.body = "图片上传成功"
        const { file } = ctx.request.files
        const fileType = ['image/jpeg', 'image/png', 'image/gif']
        if (file) {
            if (!fileType.includes(file.mimetype)) {
                return ctx.body = {
                    code: 500,
                    message: '图片格式不正确',
                    data: ""
                }
            } else {
                ctx.body = {
                    code: 200,
                    message: '图片上传成功',
                    data: {
                        url: `http://${SYS_LOCATION}:${APP_PORT}/${path.basename(file.filepath)}`
                    }
                }
            }
        } else {
            ctx.body = {
                code: 500,
                message: '图片上传失败',
                data: ""
            }
        }
    }
    // 发布轮播图
    async addBanner(ctx) {
        try {
            const findresult = await getBanner(ctx.request.body.bannername)
            if (findresult) {
                ctx.body = {
                    code: 500,
                    message: '轮播图已存在',
                    data: ""
                }
                return
            } else {
                const res = await bannerAdd(ctx.request.body)
                ctx.body = {
                    code: 200,
                    message: '添加轮播图成功!',
                    data: res
                }
            }
        } catch (err) {
            ctx.body = {
                code: 500,
                message: '添加轮播图失败',
                data: err
            }
            return
        }
    }
    // 修改轮播图信息
    async updateBanner(ctx) {
        try {
            const res = await bannerUpdate(ctx.request.body)
            if (res) {
                ctx.body = {
                    code: 200,
                    message: '更新轮播图成功',
                    data: ''
                }
            } else {
                ctx.body = {
                    code: 500,
                    message: '更新轮播图失败',
                    data: ""
                }
            }
        } catch (err) {
            ctx.body = {
                code: 500,
                message: '更新轮播图失败',
                data: err
            }
            return
        }
    }
    // 删除轮播图
    async deleteBanner(ctx) {
        try {
            const res = await bannerDelete(ctx.request.body.id)
            if (res) {
                ctx.body = {
                    code: 200,
                    message: '删除轮播图成功',
                    data: ''
                }
            } else {
                ctx.body = {
                    code: 500,
                    message: '删除轮播图失败',
                    data: ""
                }
            }
        } catch (err) {
            ctx.body = {
                code: 500,
                message: '删除轮播图失败',
                data: err
            }
        }
    }
    // 获取详情
    async getBannerDetail(ctx) {
        try {
            const res = await bannerDetail(ctx.request.body.id)
            if (res) {
                ctx.body = {
                    code: 200,
                    message: '获取轮播图详情成功',
                    data: res
                }
            } else {
                ctx.body = {
                    code: 500,
                    message: '获取轮播图详情失败',
                    data: ""
                }
            }
        } catch (err) {
            ctx.body = {
                code: 500,
                message: '获取轮播图详情失败',
                data: err
            }
        }
    }
    // 获取轮播图列表
    async getBannerList(ctx) {
        try {
            const { res, total } = await bannerList(ctx.request.body.page, ctx.request.body.pageSize, ctx.request.body)
            if (res) {
                ctx.body = {
                    code: 200,
                    message: '获取轮播图列表成功',
                    data: res,
                    total: total
                }
            } else {
                ctx.body = {
                    code: 500,
                    message: '获取轮播图列表失败',
                    data: ""
                }
            }
        } catch (err) {
            ctx.body = {
                code: 500,
                message: '获取轮播图列表失败',
                data: err
            }
        }
    }
}
module.exports = new BannerController()